﻿using lwzlz.Application.System;
using lwzlz.Web.Areas.Api.Models.Teacher;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Web.Http;

namespace lwzlz.Web.Areas.Api.Controllers
{
    /// <summary>
    /// 老师接口
    /// </summary>
    [RoutePrefix("Api/Teacher")]
    public class TeacherController : ApiController
    {
        [HttpPost]
        [Route("Login")]
        public ResponseModel Login(LoginInput input)
        {
            var response = new ResponseModel() { Success = false };
            try
            {
                var teacherService = new TeacherService();
                var teacher = teacherService.GetModel(input.UserName);
                if (teacher != null)
                {
                    if (teacher.Status == 1)
                    {
                        var store = new StoreService().Get(teacher.StoreId);
                        if (store != null)
                        {
                            if (store.Status == 1)
                            {
                                if (teacher.PassWord == new Common.Symmetric().Encrypto(input.PassWord))
                                {
                                    //MAC地址不为空，校验MAC地址
                                    if (!string.IsNullOrWhiteSpace(teacher.MacAddress))
                                    {
                                        if (teacher.MacAddress == input.MacAddress)
                                        {
                                            var result = teacherService.UpdateLoginTime(teacher.Id);
                                            if (result.Success)
                                            {
                                                response.Success = true;
                                                response.Data = new LoginOutput()
                                                {
                                                    UserName = teacher.UserName,
                                                    RealName = teacher.RealName,
                                                    Phone = teacher.Phone,
                                                    Email = teacher.Email,
                                                    MacAddress = teacher.MacAddress,
                                                    CreationTime = teacher.CreationTime,
                                                    StoreName = store.Name,
                                                    StoreAddress = store.Address,
                                                    JoinTime = store.JoinTime ?? store.CreationTime
                                                };
                                            }
                                            else
                                            {
                                                throw new Exception(result.Message);
                                            }
                                        }
                                        else
                                        {
                                            throw new Exception("MAC地址错误");
                                        }
                                    }
                                    //MAC地址为空，记录MAC地址
                                    else
                                    {
                                        teacher.MacAddress = input.MacAddress;
                                        teacher.LastLoginTime = DateTime.Now;
                                        var result = teacherService.Update(teacher, null);
                                        if (result > 0)
                                        {
                                            response.Success = true;
                                            response.Data = new LoginOutput()
                                            {
                                                UserName = teacher.UserName,
                                                RealName = teacher.RealName,
                                                Phone = teacher.Phone,
                                                Email = teacher.Email,
                                                MacAddress = teacher.MacAddress,
                                                CreationTime = teacher.CreationTime,
                                                StoreName = store.Name,
                                                StoreAddress = store.Address,
                                                JoinTime = store.JoinTime ?? store.CreationTime
                                            };
                                        }
                                        else
                                        {
                                            throw new Exception("记录MAC地址失败");
                                        }
                                    }
                                }
                                else
                                {
                                    throw new Exception("密码错误");
                                }
                            }
                            else
                            {
                                throw new Exception("门店已被禁用");
                            }
                        }
                        else
                        {
                            throw new Exception("门店信息为空");
                        }
                    }
                    else
                    {
                        throw new Exception("账号已被禁用");
                    }
                }
                else
                {
                    throw new Exception("账号信息为空");
                }
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }
            return response;
        }
    }
}